#include "chatcommand_simplegameusers.h"
#include "websocket.h"
#include "datastring.h"
#include "parameters.h"
#include "stringbuilder.h"
#include "message.h"
#include "Debug.h"
#include <inttypes.h>
#include "simplechatgame.h"

// simplegameusers(messageid,gameid)
// Returns the list of users in a game. Returns a simplegameusers message or an error message.

bool chatcommand_simplegameusers::processmessage(char first_letter,message *received_message,chatclient *client)
{
	if ((first_letter != 's') 
		|| (received_message->actual_message.substr(0,16)!="simplegameusers(")) {
		return false;
	}	
	
	Debug debug(__FILE__,__func__,__LINE__);
	message *new_message;
	datastring method_parameters;
	parameters parameters_parsed;
	bool parameter_success = true;
	int64_t messageid;
	datastring gameid;	
	biglist_item<simplechatgame *> *gamesearch;
	simplechatgame *game;
	datastring error_message;
	
	debug = __LINE__;
	method_parameters = received_message->actual_message.substr(16,received_message->actual_message.length-17);
	method_parameters.null_terminate(); // Used to comply with snprintf.
	parameters_parsed.long_parameter(method_parameters,parameter_success);
	parameters_parsed.string_parameter(method_parameters,parameter_success);
		
	debug = __LINE__;
	if (parameter_success) {
		// Give the parameters nice names.
		debug = __LINE__;
		messageid = parameters_parsed.long_parameters[0];
		gameid = parameters_parsed.string_parameters[1];		
	
		debug = __LINE__;		
		// Find the game.
		gamesearch = the_websocket->find_simple_game(gameid);
		if (gamesearch == nullptr) {
			error_message = "That gameid was not found.";
			error(client,error_message,messageid);
			return true;
		}
		game = gamesearch->item;
		debug = __LINE__;
		new_message = simplegameusers(messageid,gameid,&(game->users));
		client->push_message(&new_message);
		debug = __LINE__;
	} else {
		debug = __LINE__;
		parameters_not_correct(client,received_message);
	}
	debug = __LINE__;
	return true;
}
